//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing.Diagram
{
    internal class StyleDefinitionHeader : IOfficeXmlSerializable
    {
        internal const string xmlName = "styleDefHdr";
        private List<SDDescription> _descriptions = new List<SDDescription>();
        private List<SDName> _titles = new List<SDName>();
        private List<SDCategory> catLst = new List<SDCategory>();
        private OfficeArtExtensionList extLst;
        private string minVer = "http://schemas.openxmlformats.org/drawingml/2006/diagram";
        private int resId;
        private string uniqueId;

        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read(xmlName))
            {
                uniqueId = c.GetString("uniqueId");
                minVer = c.GetOptionalString("minVer", "http://schemas.openxmlformats.org/drawingml/2006/diagram");
                resId = c.GetOptionalInteger("resId", 0);
                _titles.Clear();
                _titles.AddRange(c.ReadOptionalWhile<SDName>("title"));
                _descriptions.Clear();
                _descriptions.AddRange(c.ReadOptionalWhile<SDDescription>(SDDescription.xmlName));
                catLst.Clear();
                catLst.AddRange(c.ReadElements<SDCategory>("catLst", "cat"));

                extLst = c.ReadOptionalElement<OfficeArtExtensionList>("extLst");
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write(xmlName))
            {
                c.SetString("uniqueId", uniqueId );
                c.SetOptionalString("minVer", minVer, "http://schemas.openxmlformats.org/drawingml/2006/diagram" );
                c.SetOptionalInteger("resId", resId, 0 );
                c.WriteElements(_titles);
                c.WriteElements(_descriptions);
                if (catLst.Count > 0)
                {
                    using (var c2 = c.Write("catLst"))
                    {
                        c2.WriteElements(catLst);
                    }
                }
                c.WriteOptionalElement(extLst );
            }
        }

        #endregion
    }
}